package com.codeoss.framework.view.pagebean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.view.clientscript.SearchDialogCallbackClientScript;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.Tree;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.HierarchyData;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.pagebean.crud.PageBeanCRUDUtil;
import com.codeoss.framework.view.pagebean.crud.SelectRequest;
import com.codeoss.framework.view.pagebean.crud.SelectResponse;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.framework.view.util.PageBeanUtil;

@Service
public class SearchTreeViewPageBean extends BasePageBean {
	public static final String KEY_TREE = "__tree__";
	@Autowired
	private IVariableService variableService;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private PageBeanCRUDUtil pageBeanCRUDUtil;
	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		if (page.isInit()) {
			return;
		}
		pageBeanUtil.getComponent(yunContext, page, "tree");
		Tree tree=(Tree)yunContext.getAttribute("__component__");
		page.setAttribute(KEY_TREE, tree);
		page.setInit(true);
	}
	public void init() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		Tree tree=(Tree)page.getAttribute(KEY_TREE);
		
		DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(page, tree.getDataSource());
		Table table=tableStore.get(dataSourceConfig.getTableName());
		
		SelectRequest selectRequest=new SelectRequest();
		selectRequest.setMainTable(table);
		Query query=new Query();
		selectRequest.setMainQuery(query);
		SelectResponse selectResponse=pageBeanCRUDUtil.select(yunContext, selectRequest);
		
		HierarchyData root=dataSourceUtil.convertTableDataToHierarchyData(yunContext, selectResponse.getTableDataMap().get(table.getTableName()).getData());
		yunContext.setAttribute(tree.getId()+"_data", root);
	}
	
	public void onTreeItemClickHandler() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		Tree tree=(Tree)page.getAttribute(KEY_TREE);
		Object treeId=variableService.getVariableValue(yunContext, tree.getVarName());
		SearchDialogCallbackClientScript searchDialogCallbackClientScript=new SearchDialogCallbackClientScript();
		searchDialogCallbackClientScript.setSelectedValue(treeId);
		yunContext.addClientScript(searchDialogCallbackClientScript);
		yunContext.setRenderCompleted(true);
	}
}
